import { Module } from 'vuex'
import { RootState } from '../types'
import { dashboardModule } from './types'

import {
  getCategoryGoodsCount,
  getCategoryGoodsSale,
  getCategoryGoodsFavor,
  getAddressGoodsSale
} from '@/api/dashboard/dashboard'

const dashboardStore: Module<dashboardModule, RootState> = {
  namespaced: true,
  state: () => ({
    categoryGoodsCount: [],
    categoryGoodsSale: [],
    categoryGoodsFavor: [],
    categoryGoodsTop10: [],
    addressGoodsSale: [],
    amountGoodsList: []
  }),
  mutations: {
    SET_CATEGORY_GOODS_COUNT(state, list) {
      state.categoryGoodsCount = list
    },
    SET_CATEGORY_GOODS_SALE(state, list) {
      state.categoryGoodsSale = list
    },
    SET_CATEGORY_GOODS_FAVOR(state, list) {
      state.categoryGoodsFavor = list
    },
    SET_ADDRESS_GOODS_SALE(state, list) {
      state.addressGoodsSale = list
    }
  },
  actions: {
    async getDashboardGoodsCountAction({ commit }) {
      const categoryCountResult = await getCategoryGoodsCount()
      console.log(categoryCountResult)
      commit('SET_CATEGORY_GOODS_COUNT', categoryCountResult.data)
    },
    async getDashboardGoodsSaleAction({ commit }) {
      const categorySaleResult = await getCategoryGoodsSale()
      commit('SET_CATEGORY_GOODS_SALE', categorySaleResult.data)
    },
    async getDashboardGoodsFavorAction({ commit }) {
      const categoryFavorResult = await getCategoryGoodsFavor()
      commit('SET_CATEGORY_GOODS_FAVOR', categoryFavorResult.data)
    },
    async getAddressGoodsSaleAction({ commit }) {
      const addressSaleResult = await getAddressGoodsSale()
      commit('SET_ADDRESS_GOODS_SALE', addressSaleResult.data)
    }
  },
  getters: {}
}

export default dashboardStore
